package com.teaxiaoyi.tea_xiaoyi.controller;

import com.teaxiaoyi.tea_xiaoyi.common.AppResult;
import com.teaxiaoyi.tea_xiaoyi.common.ResultCode;
import com.teaxiaoyi.tea_xiaoyi.model.employee;
import com.teaxiaoyi.tea_xiaoyi.services.IEmployeeService;
import com.teaxiaoyi.tea_xiaoyi.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
//对controller进行api说明
@Api (tags = "用户接口")
//日志注解
@Slf4j
//这是一个返回数据的controller
@RestController
//路由映射
@RequestMapping("/employee")
public class UserController {
    @Resource
    private IEmployeeService employeeService;

    /**
     * 用户注册
     * @param username  用户名
     * @param name      用户昵称
     * @param password  用户密码
     * @param passwordRepeat    重复密码
     * @return  返回
     */
    @ApiOperation("用户注册")
    @PostMapping ("/register")
    public AppResult register( @ApiParam("用户名") @RequestParam("username")@NonNull String username,
                               @ApiParam("昵称")@RequestParam("name")@NonNull String name,
                               @ApiParam("密码")@RequestParam("password")@NonNull String password,
                               @ApiParam("确认密码")@RequestParam("passwordRepeat")@NonNull String passwordRepeat) {

        //传统的数据校验
//        //数据校验
//        if (StringUtil.isEmpty(username) || StringUtil.isEmpty(name) || StringUtil.isEmpty(password)
//                || StringUtil.isEmpty(passwordRepeat)) {
//        //返回错误信息
//            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
//        }
    //校验密码与确认密码是否相同
    if (!password.equals(passwordRepeat)) {
        log.warn(ResultCode.FAILED_TWO_PWD_NOT_SAME.toString());
        return AppResult.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME);
    }

//  准备数据
    employee employee = new employee();
    employee.setUsername(username);
    employee.setName(name);
    employee.setPassword(password);
    //调用service
    employeeService.createEmployee(employee);
    //返回成功
    return AppResult.success();
    }
}







